1 /* 2 * Copyright (C) 2007 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.common.collect; 18 19 import java.util.HashMap; 20 import java.util.Map; 21 22 import javax.annotation.Nullable; 23 24 /** 25 * GWT emulation of {@code HashBiMap} that just delegates to two HashMaps. 26 * 27 * @author Mike Bostock 28 */ 29 public final class HashBiMap<K, V> extends AbstractBiMap<K, V> { 30 31 /** 32 * Returns a new, empty {@code HashBiMap} with the default initial capacity 33 * (16). 34 */ 35 public static <K, V> HashBiMap<K, V> create() { 36 return new HashBiMap<K, V>(); 37 } 38 39 /** 40 * Constructs a new, empty bimap with the specified expected size. 41 * 42 * @param expectedSize the expected number of entries 43 * @throws IllegalArgumentException if the specified expected size is 44 * negative 45 */ 46 public static <K, V> HashBiMap<K, V> create(int expectedSize) { 47 return new HashBiMap<K, V>(expectedSize); 48 } 49 50 /** 51 * Constructs a new bimap containing initial values from {@code map}. The 52 * bimap is created with an initial capacity sufficient to hold the mappings 53 * in the specified map. 54 */ 55 public static <K, V> HashBiMap<K, V> create( 56 Map<? extends K, ? extends V> map) { 57 HashBiMap<K, V> bimap = create(map.size()); 58 bimap.putAll(map); 59 return bimap; 60 } 61 62 private HashBiMap() { 63 super(new HashMap<K, V>(), new HashMap<V, K>()); 64 } 65 66 private HashBiMap(int expectedSize) { 67 super( 68 Maps.<K, V>newHashMapWithExpectedSize(expectedSize), 69 Maps.<V, K>newHashMapWithExpectedSize(expectedSize)); 70 } 71 72 // Override these two methods to show that keys and values may be null 73 74 @Override public V put(@Nullable K key, @Nullable V value) { 75 return super.put(key, value); 76 } 77 78 @Override public V forcePut(@Nullable K key, @Nullable V value) { 79 return super.forcePut(key, value); 80 } 81 }